﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace weighing
{

    /// <summary>
    /// Data Grid Columns Helper
    /// </summary>
    public class DataGridColumnsHelper
    {
        public static string inputName = "inputName";
        public static string outputName = "outputName";
        public static string othreName = "othreName";
        public static string reportBuy = "reportBuy";
        public static string reportSale = "reportSale";
        public static string reportSelf = "reportSelf";
        public static string reportforegin = "reportforegin";
        public static string mixture = "mixture";
        /// <summary>
        /// 简易模式，自定义输入项目UI
        /// </summary>
        public static string SimpleModeUI = "SimpleModeUI";

        private static Dictionary<string, Dictionary<string, bool>> _columnsSettings;


        public static Dictionary<string, Dictionary<string, bool>> ColumnsSettings
        {
            get
            {
                if (_columnsSettings == null)
                {
                    _columnsSettings = loadFromFile();
                }
                return _columnsSettings;
            }
        }


        public static Dictionary<string, bool> GetByName(string name)
        {
            Dictionary<string, bool> dic = null;
            if (ColumnsSettings.Keys.Contains(name))
            {
                dic = ColumnsSettings[name];
            }           
            return dic ?? new Dictionary<string, bool>();
        }


        public static void Save(string name, Dictionary<string, bool> dic)
        {
            if (ColumnsSettings.Keys.Contains(name))
            {
                _columnsSettings[name] = dic;
            }
            else
            {
                _columnsSettings.Add(name, dic);
            }

            SaveTofile();
        }


        public static void Save(string name,string header ,bool isShow)
        {
            var dic = GetByName(name);

            if (dic.Keys.Contains(header))
            {
                dic[header] = isShow;
            }
            else
            {
                dic.Add(header, isShow);
            }

            if (ColumnsSettings.Keys.Contains(name))
            {
                _columnsSettings[name] = dic;
            }
            else
            {
                _columnsSettings.Add(name, dic);
            }        

            SaveTofile();
        }


        private static Dictionary<string, Dictionary<string, bool>> loadFromFile()
        {
            string file = Constant.columnsSettingFile;

            if (!FileHelper.Exists(file))
            {
                return new Dictionary<string, Dictionary<string, bool>>();
            }
            else
            {
                string json = FileHelper.Reader(file,Encoding.UTF8);
                if (string.IsNullOrEmpty(json))
                {
                    return new Dictionary<string, Dictionary<string, bool>>();
                }

                var dic = JsonHelper.JsonToObject<Dictionary<string, Dictionary<string, bool>>>(json);

                return dic ?? new Dictionary<string, Dictionary<string, bool>> ();
            }

        }


        private static void SaveTofile()
        {
            var json = JsonHelper.ObjectToJson(ColumnsSettings);

            if (!FileHelper.Exists(Constant.columnsSettingFile))
            {
                FileHelper.CreateFile(Constant.columnsSettingFile);
            }

            FileHelper.Write(Constant.columnsSettingFile, json);
        }

    }
}
